package com.xuetang9.t9.hotel.mapper.sql.impl;

import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.FoodCondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.ReservationCondition;
import com.xuetang9.t9.hotel.mapper.sql.IMapperSql;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class FoodMapperSql implements IMapperSql {
    @Override
    public String selectByAll(Map map) {
        ICondition key = (ICondition) map.get("key");
        return getSql("*",key,false);
    }

    @Override
    public String selectByCount(Map map) {
        ICondition key = (ICondition) map.get("key");
        return getSql("COUNT(*)",key,false);
    }

    public String updateClear(Map map){
        ICondition key = (ICondition) map.get("key");
        if(key == null) {
            return null;
        }
        FoodCondition reservationCondition = null;
        if(key instanceof FoodCondition){
            reservationCondition = (FoodCondition)key;
        }else{
            throw new RuntimeException("condition type Don't match");
        }
        SQL sql = new SQL();
        sql.UPDATE("e_food");
        if(reservationCondition.getUpdateClean() != null){
            sql.SET("clear = "+reservationCondition.getUpdateClean());
        }
        if(reservationCondition.getClean() != null){
            sql.WHERE("clear = "+reservationCondition.getClean());
        }
        if(reservationCondition.getTypeId() != null && reservationCondition.getTypeId() != -1){
            sql.WHERE("category = "+reservationCondition.getTypeId());
        }
        if(reservationCondition.getName() != null && !reservationCondition.getName().isEmpty()){
            sql.WHERE("name LIKE '%"+reservationCondition.getName()+"%'");
        }
        if(reservationCondition.getId() != null && !reservationCondition.getId().isEmpty()){
            sql.WHERE("id = "+reservationCondition.getId()+"");
        }
        return sql.toString();
    }

    private String getSql(String head, ICondition condition, boolean isPage){
        SQL sql = new SQL();
        sql.SELECT(head).FROM("e_food");
        if(condition == null) {
            return sql.toString();
        }
        FoodCondition reservationCondition = null;
        if(condition instanceof FoodCondition){
            reservationCondition = (FoodCondition)condition;
        }else{
            throw new RuntimeException("condition type Don't match");
        }
        if(reservationCondition == null) {
            return sql.toString();
        }
        if(reservationCondition.getClean() != null){
            sql.WHERE("clear = "+reservationCondition.getClean());
        }
        if(reservationCondition.getTypeId() != null && reservationCondition.getTypeId() != -1){
            sql.WHERE("category = "+reservationCondition.getTypeId());
        }
        if(reservationCondition.getName() != null && !reservationCondition.getName().isEmpty()){
            sql.WHERE("name LIKE '%"+reservationCondition.getName()+"%'");
        }
        if(reservationCondition.getId() != null && !reservationCondition.getId().isEmpty()){
            sql.WHERE("id LIKE '%"+reservationCondition.getId()+"%'");
        }
        return sql.toString();
    }
}
